package com.br.alex.view.adapter;

import java.util.ArrayList;
import java.util.TreeSet;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.br.alex.view.R;

public class MenuAdapter extends BaseAdapter {
	private static final int TYPE_ITEM = 0;
	private static final int TYPE_SEPARATOR = 1;
	private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

	private ArrayList<String> mData = new ArrayList<String>();
	private LayoutInflater mInflater;
	private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
	private Context ctx;

	public Context getCtx() {
		return ctx;
	}

	public void setCtx(Context ctx) {
		this.ctx = ctx;
	}

	//	public MenuAdapter(Context context) {
	public MenuAdapter(Context ctx) {
		super();
		this.ctx = ctx;
		mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	//	}

	public void addItem(final String item) {
		mData.add(item);
		notifyDataSetChanged();
	}

	public void addSeparatorItem(final String item) {
		mData.add(item);
		// save separator position
		mSeparatorsSet.add(mData.size() - 1);
		notifyDataSetChanged();
	}

	@Override
	public int getItemViewType(int position) {
		return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
	}

	@Override
	public int getViewTypeCount() {
		return TYPE_MAX_COUNT;
	}

	public int getCount() {
		return mData.size();
	}

	public String getItem(int position) {
		return mData.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		int type = getItemViewType(position);
		System.out.println("getView " + position + " " + convertView + " type = " + type);
		if (convertView == null) {
			holder = new ViewHolder();
			switch (type) {
			case TYPE_ITEM:
				convertView = mInflater.inflate(R.layout.header1, null);
				holder.textView = (TextView)convertView.findViewById(R.id.titulo);
								Typeface font = Typeface.createFromAsset(
									ctx.getAssets(), "optimabold.ttf");
								holder.textView.setTypeface(font);
				holder.textViewLegenda = (TextView)convertView.findViewById(R.id.textLegenda);
								holder.textViewLegenda.setTypeface(font);
				break;
			case TYPE_SEPARATOR:
				convertView = mInflater.inflate(R.layout.header, null);
				holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);


				break;
			}
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder)convertView.getTag();
		}
		holder.textView.setText(mData.get(position));

		return convertView;
	}
	public static class ViewHolder {
		public TextView textView;
		public TextView textViewLegenda;
	}


}
